package grp.test.controller;

import grp.test.pojo.Borrow;
import grp.test.pojo.Book;
import grp.test.pojo.BorrowKey;
import grp.test.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by 明婵度 on 2020/8/15.
 */
@Controller
@RequestMapping("/borrow/")
public class BorrowController {

    @Autowired
    private IBorrowService iBorrowService;

    //查询
    @RequestMapping(value = "searchBookById.do",method = RequestMethod.POST)
    @ResponseBody
    public Book getNameByID(Integer bookId){
        Book obj = iBorrowService.SearchBookById(bookId);
        if(obj ==null){
            return null;
        }
        return obj;
    }
//    @RequestMapping(value = "searchBookByName.do",method = RequestMethod.POST)
//    @ResponseBody
//    public Book getNameByName(String bookName) throws UnsupportedEncodingException {
//        String realName = java.net.URLEncoder.encode(bookName,"utf-8");
//        Book obj = iBorrowService.SearchBookByName(realName);
//        if(obj ==null){
//            return null;
//        }
//        return obj;
//
//    }

    //借阅记录
    @RequestMapping(value = "userRecord.do",method = RequestMethod.POST)
    @ResponseBody
    public Borrow BorrowLook(Integer userId,Integer bookId)
    {
        BorrowKey key = new BorrowKey(userId,bookId);
        Borrow obj = iBorrowService.LookMine(key);
        if (obj==null)
        {
            return null;
        }
        else
            return obj;
    }

    //借阅
    @RequestMapping(value = "borrowBook.do",method = RequestMethod.POST)
    @ResponseBody
    public List<String> BorrowBook(Integer bookId,Integer userId)
    {
        List<String> lst = iBorrowService.BorrowBook(bookId,userId);
        return lst;
    }

    //续借
    @RequestMapping(value = "renew.do",method = RequestMethod.POST)
    @ResponseBody
    public Borrow renew(Integer userId,Integer bookId)
    {
        BorrowKey key = new BorrowKey(userId,bookId);
        Borrow obj = iBorrowService.RenewBook(key);
        if (obj==null)
        {
            return null;
        }
        else
            return obj;
    }



}
